<template>
  <list-layout>
    <div  slot="search">
      <el-form :model="queryParams" ref="queryForm" label-position="top"  v-show="showSearch" label-width="68px">
        <el-form-item :label="$t('message.messageName')" prop="noticeTitle">
          <el-input
            v-model="queryParams.noticeTitle"
            :placeholder="$t('public.placeholder',{placeholder: $t('message.messageName')})"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item :label="$t('message.messageType')" prop="noticeType">
          <el-select
            v-model="queryParams.noticeType"
            :placeholder="$t('public.placeholder',{placeholder: $t('message.messageType')})"
            style="width:100%;"
            clearable
            size="small"
          >
            <el-option
              key="1"
              :label="$t('message.notice')"
              value="1"
            />
            <el-option
              key="2"
              :label="$t('message.announcement')"
              value="2"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('public.status')" prop="hasRead">
          <el-select
            v-model="queryParams.hasRead"
            :placeholder="$t('public.placeholder',{placeholder: $t('message.messageStatus')})"
            clearable
            style="width:100%;"
            size="small"
          >
            <el-option
              key="0"
              :label="$t('message.unread')"
              value="0"
            />
            <el-option
              key="1"
              :label="$t('message.read')"
              value="1"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('public.createTime')">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width:100%"
            value-format="yyyy-MM-dd"
            type="daterange" autocomplete="off"
            range-separator="-"
            :start-placeholder="$t('public.startDate')"
            :end-placeholder="$t('public.endDate')"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('public.search')}}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
        </el-form-item>
      </el-form>
    </div>
  <div class="app-container">


<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:dict:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-refresh"-->
<!--          size="mini"-->
<!--          @click="handleRefreshCache"-->
<!--          v-hasPermi="['system:dict:remove']"-->
<!--        >刷新缓存</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table stripe v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">

      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('public.opera')" fixed="left" align="center" width="100">
        <template slot-scope="scope">
          <div  class="action-wrap">
            <table-body-btn type="delete" @click.native="handleDelete(scope.row)"    v-hasPermi="['system:message:remove']"/>
            <table-body-btn type="view" @click.native="handleView(scope.row)"    v-hasPermi="['system:message:view']"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('message.messageNum')" align="center" prop="noticeId" width="100" />
      <el-table-column :label="$t('message.messageName')" align="center" prop="noticeTitle" :show-overflow-tooltip="true" />
      <el-table-column :label="$t('message.messageType')" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
            <span>{{ scope.row.noticeType == '1'?$t('message.notice'):$t('message.announcement') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('public.status')" align="center" prop="hasRead">
        <template slot-scope="scope">
          {{scope.row.hasRead == 0 ?$t('message.unread'):$t('message.read')}}
        </template>
      </el-table-column>
      <el-table-column :label="$t('public.remark')" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column :label="$t('public.createTime')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="form.noticeTitle" :visible.sync="open" center top="34vh" width="500px" append-to-body :fullscreen="fullscreen">
      <p style="text-align: center">{{form.noticeContent}}</p>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" icon="el-icon-check" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
  </list-layout>
</template>

<script>
import { listType, getType, delType, addType, updateType, refreshCache } from "@/api/basic/dict/type";
import { getNoticeList, deleteNotice, readNotice } from "@/api/system/message";

export default {
  name: "Dict",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        noticeType: undefined,
        hasRead: undefined,
        createTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" }
        ]
      },
      fullscreen: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      getNoticeList(this.queryParams).then(response=>{

        if(response.code == 200 && response.rows){
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictId = row.dictId || this.ids
      getType(dictId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字典类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.open = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.noticeId || this.ids;
      this.$modal.confirm('是否确认删除消息编号为"' + dictIds + '"的数据项？').then(function() {
        return deleteNotice(dictIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('tips.deleteSuccess'));
      }).catch(() => {});
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.form = row
      this.open = true
      if(row.hasRead == 0){
        readNotice(row.noticeId).then(res=>{
          if(res.code == 200){
            this.handleQuery()
          }
        })
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    }
  }
};
</script>
